{% extends "_base.html" %}


{% block  title %}容器编排列表{% endblock %}

{% block left %}
    {% include "_left.html" with cmdb_docker_compose_active="active" %}
{% endblock left %}

{% block page-content %}


    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>资产管理</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="/">主页</a>
                </li>
                <li>容器编排管理</a>
                </li>
                <li class="active">
                    <strong>容器组列表</strong>
                </li>
            </ol>
        </div>
    </div>

    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>通过Docker-Compose创建的容器组-列表</h5>
                    </div>
                    <div class="ibox-content">

                        <div class="table-responsive">

                            <a href="{% url 'cmdb:docker_compose_add' %}" class="btn btn-primary">新建容器编排</a>
                            {% if filter %}({{ filter }} - <a href="{% url 'cmdb:docker_compose_list' %}">清除过滤条件</a>){% endif %}

                            <table class="table table-striped table-bordered table-hover" id="do">
                                <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>YML文件</th>
                                    <th>宿主机</th>
                                    <th>数量参数</th>
                                    <th>最后修改</th>
                                    <th class="col-lg-3">容器操作</th>
                                    <th class="col-lg-2">动作</th>
                                </tr>
                                </thead>
                                <tbody>

                                {% for row in object_list %}

                                    <tr {% if not row.yml.state %}style="color:#ccc"{% endif %}>
                                        <td class="center">
                                            <a href="{% url 'cmdb:docker_compose_edit' row.id %}">{{ row.name }}</a>
                                        </td>
                                        <td class="center">{{ row.yml }}</td>
                                        <td class="center">{{ row.dockerhost }}</td>
                                        <td class="center">{{ row.scale }}</td>
                                        <td class="center">{{ row.changetime }}</td>
                                        <td>
                                            <button type="button" class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal">状态</button>
                                            <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">启动</button>
                                            <button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myModal">停止</button>
                                            <button type="button" class="btn btn-success btn-xs" data-toggle="modal" data-target="#myModal" title="执行docker-compose up命令">进程</button>
                                            <button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal" title="执行docker-compose down命令">删除</button>
                                        </td>
                                        <td>
                                            <a class="btn btn-success btn-xs " href="/admin/cmdb/dockercompose/{{ row.id }}/change/" target="_blank">编辑</a>
                                            <a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#myModal" onclick="javascript:del_obj(this)">删除</a>
                                        </td>
                                    </tr>
                                {% endfor %}

                                </tbody>

                            </table>

                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- 操作返回 - 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="false">
                        &times;
                    </button>
                    <span id="server"></span>
                </div>
                <div class="modal-body">
                    <textarea id="server_msg" readonly style="width:100%;" rows="5">{{ text }}</textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                </div>
            </div>
        </div>
    </div>


{% endblock page-content %}



{% block footer-js %}
    <script>
        function conn_docker(url, textarea_height){
            $.ajax({
                url: url,
                type: 'GET',
                timeout: 20000,
                error: function(){
                    $("#server_msg").val('网站连接失败！请联系CMDB管理员');
                },
                success: function(msg_html){
                    // json格式化
                    // console.log(msg_html);
                    $('#server_msg').val(msg_html);
                    // console.log(txt);
                    $('#server_msg')[0].style.minHeight= textarea_height;

                }
            });
        };

        $(document).ready(function(){

            // $( ".btn-danger.btn-xs" ).click()
            $('#do').on('click', 'button', function (e) {
                // console.log($(this));
                var url=this.parentElement.parentElement.children[0].children[0].href+"do/";
                var textarea_height="200px";
                $("#server_msg").val('连接中……');
                if (this.className=="btn btn-danger btn-xs") {
                    if(! window.confirm('确认停止及删除对应的容器？')){
                        return false;
                    }
                    textarea_height="100px";
                    url=url+"?do=down";
                } else if (this.className=="btn btn-primary btn-xs") {
                    url=url+"?do=start";
                } else if (this.className=="btn btn-success btn-xs") {
                    url=url+"?do=top";
                } else if (this.className=="btn btn-info btn-xs") {
                    url=url+"?do=ps";
                } else if (this.className=="btn btn-warning btn-xs") {
                    url=url+"?do=stop";
                }

                $("#server").text(this.parentElement.parentElement.children[0].textContent+" "+this.textContent);
                // console.log(url);
                conn_docker(url, textarea_height);

            });

        });


    function conn_del(url){
        // 提交删除
        url=url+'del/';
        // console.log(url);
        // return false;
        $.ajax({
            url: url,
            type: 'GET',
            timeout: 20000,
            error: function(){
                $("#server_msg").val('网站连接失败！请联系CMDB管理员');
            },
            success: function(msg_html){
                $("#server_msg").val(msg_html);
                var msg = JSON.parse(msg_html);
                // console.log(msg.remove);
                if (msg.remove=="ok") {
                    // 删除成功，刷新页面
                    location.reload()
                }
            }
        });

    }

    function del_obj(obj){
        // 单个删除
        // console.log($(obj));
        var url=obj.parentElement.parentElement.children[0].children[0].href;
        if(! window.confirm('确认删除？它所编排的容器组不会删除。')){
            return false;
        }
        $("#server").text(obj.parentElement.parentElement.children[1].textContent+" "+obj.textContent);
        return conn_del(url);
    }

    </script>

{% endblock footer-js %}

